<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";

const myColor = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6","#91cd76"];

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "200px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(this.chartData);
    },

    setOptions(data) {
      this.chart.setOption({
        grid: {
          top: "10%",
          left: "22%",
          bottom: "10%",
        },
        xAxis: {
          show: false,
        },
        yAxis: [
          {
            type: "category",
            data: ["涉黄", "舆论", "涉政", "涉暴","恐怖","潜在"],
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
          },
          {
            data: [100, 100, 100, 100,100,100],
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              color: "#fff",
            },
          },
        ],
        series: [
          {
            name: "条",
            type: "bar",
            yAxisIndex: 0,
            data: data,
            itemStyle: {
              barBorderRadius: 20,
              color: function (params) {
                //params是传进来的柱子对象
                return myColor[params.dataIndex];
              },
            },
            barCategoryGap: 50,
            barWidth: 10,
            //显示柱子内的文字
            label: {
              align:"center",
              show: true,
              // position: "inside",
              //{c}会自动解析data里的数据
              formatter: "{c}%",
            },
          },
          {
            name: "框",
            type: "bar",
            yAxisIndex: 1,
            barCategoryGap: 50,
            barWidth: 15,
            data: [100, 100, 100, 100, 100,100],
            itemStyle: {
              color: "none",
              borderColor: "#00c1de",
              borderWidth: 3,
              barBorderRadius: 15,
            },
          },
        ],
      });
    },
  },
};
</script>
